package com.zhiqianbao.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by liang on 2016/2/2.
 */
public class ScreenManager {
    private static Stack<Activity> activityStack;
    private static ScreenManager instance;
    private ScreenManager() {

    }
    public static ScreenManager getScreenManager() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;
    }

    //将当前Activity推入栈中
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        if(!activityStack.contains(activity)) {
            activityStack.add(activity);
        }
    }

    //退出栈中所有Activity
    public void popAllActivityExceptOne() {
        while (activityStack.size()!=0) {
            activityStack.get(0).finish();
            activityStack.remove(0);
        }
    }
    //退出栈中制定类Activity
    public void popItemActivityExceptOne(Class cls) {
        int i=0;
        while (true) {
            if(i<activityStack.size()){
                if(activityStack.get(i).getClass()==cls){
                    activityStack.get(i).finish();
                    activityStack.remove(i);
                    break;
                }
                i++;
            }
            else{
              break;
            }
        }
    }

    //退出栈中制定类Activity
    public void popItemActivityExceptOne1(Class cls) {
        int i=0;
        while (true) {
            if(i<activityStack.size()){
                if(activityStack.get(i).getClass()==cls){
                    activityStack.remove(i);
                    break;
                }
                i++;
            }
            else{
                break;
            }
        }
    }
}
